﻿using CaseManagerLibrary.Licence;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDLicence
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();

            LoadLicence();
        }

        private void LoadLicence()
        {
            string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "licence");

            if (System.IO.Directory.Exists(path))
            {
                string[] files = System.IO.Directory.GetFiles(path, "*.pal");

                foreach (string file in files)
                {
                    string fileName = System.IO.Path.GetFileName(file).Replace(".pal", "");

                    string[] temp = fileName.Split("_".ToCharArray());

                    if (temp.Length > 0)
                    {
                        string sn = temp[1];

                        var licence = LicenceManager.GetLicenceContentFromFile(file, sn);

                        if (licence != null)
                        {
                            ListViewItem item = new ListViewItem();
                            item.Text = (lvwLicence.Items.Count + 1).ToString();
                            item.SubItems.Add(licence.Client);
                            item.SubItems.Add(licence.ProductId);
                            item.SubItems.Add(licence.SN);
                            item.SubItems.Add(licence.StartTime);
                            item.SubItems.Add(licence.EndTime);
                            item.SubItems.Add(licence.Key);
                            item.Tag = licence;

                            lvwLicence.Items.Add(item);
                        }
                    }
                }
            }
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmLicence frm = new FrmLicence();

            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.lvwLicence.Items.Add(new ListViewItem(new string[] {
                    (this.lvwLicence.Items.Count+1).ToString(),
                    frm.Licence.Client,
                    frm.Licence.ProductId,
                    frm.Licence.SN,
                    frm.Licence.StartTime,
                    frm.Licence.EndTime,
                    frm.Licence.Key
                })).Tag = frm.Licence;
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.lvwLicence.SelectedItems.Count > 0)
            {
                FrmLicence frm = new FrmLicence();
                frm.SetLicence(this.lvwLicence.SelectedItems[0].Tag as LicenceInfo);
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    this.lvwLicence.SelectedItems[0].SubItems[1].Text = frm.Licence.Client;
                    this.lvwLicence.SelectedItems[0].SubItems[2].Text = frm.Licence.ProductId;
                    this.lvwLicence.SelectedItems[0].SubItems[3].Text = frm.Licence.SN;
                    this.lvwLicence.SelectedItems[0].SubItems[4].Text = frm.Licence.StartTime;
                    this.lvwLicence.SelectedItems[0].SubItems[5].Text = frm.Licence.EndTime;
                    this.lvwLicence.SelectedItems[0].SubItems[6].Text = frm.Licence.Key;
                }
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (this.lvwLicence.SelectedItems.Count > 0)
            {
                this.lvwLicence.SelectedItems[0].Remove();
            }
        }

        private void btnFile_Click(object sender, EventArgs e)
        {
            if (this.lvwLicence.SelectedItems.Count > 0)
            {
                LicenceInfo licence = this.lvwLicence.SelectedItems[0].Tag as LicenceInfo;

                try
                {
                    string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "licence");
                    if (!System.IO.Directory.Exists(path))
                    {
                        System.IO.Directory.CreateDirectory(path);
                    }

                    LicenceManager.CreateLicenceFile(licence, System.IO.Path.Combine(path, "licence_" + licence.SN + ".pal"));
                }
                catch (Exception ex)
                { }
            }
        }
    }
}
